using Microsoft.AspNetCore.Mvc;
using test.Models;
using Common.Function;

namespace test.Controllers
{

    [ApiController]
    [Route("[controller]")]
    public class CommonController : ControllerBase
    {

        private readonly hongjiContext _db;
        public CommonController(hongjiContext db)
        {
            _db = db;
        }

        [HttpGet(Name = "CommonID")]
        public Common Get(int ID)
        {
            //int ID = 0, int ColumnID = 0, int Begin = 1, int Limit = 15
            Common Com = new();
            var CommonInfo = _db.Commontables.Where(x => x.Id == ID).FirstOrDefault();
            if (CommonInfo != null)
            {

                Com.Id = CommonInfo.Id;
                Com.ColumnId = CommonInfo.ColumnId;
                Com.ColumnName = CommonFunction.GetColumnName(_db, CommonInfo.ColumnId);
                Com.Topic = CommonInfo.Topic;
                Com.ImgUrl = CommonInfo.ImgUrl;
                Com.Contents = CommonInfo.Content;
                Com.AddTime = CommonInfo.AddTime;
            }
            return Com;
        }


        [HttpGet("Total")]
        public int Total1(int ID)
        {
            var Total = _db.Commontables.Where(x => x.ColumnId == ID);
            if (Total != null)
            {
                return Total.Count();
            }
            return 0;
        }


        [HttpGet("IDUp")]
        public Common IDUp(int ID)
        {
            Common Com = new();
            var CommonInfo1 = _db.Commontables.Where(x => x.Id == ID).FirstOrDefault();
            if (CommonInfo1 != null)
            {
                var CommonInfo = _db.Commontables.Where(x=>x.ColumnId == CommonInfo1.ColumnId && x.Id > ID).OrderBy(x=>x.Id).FirstOrDefault();
                if (CommonInfo != null)
                {
                    Com.Id = CommonInfo.Id;
                    Com.ColumnId = CommonInfo.ColumnId;
                    Com.ColumnName = CommonFunction.GetColumnName(_db, CommonInfo.ColumnId);
                    Com.Topic = CommonInfo.Topic;
                    Com.ImgUrl = CommonInfo.ImgUrl;
                    Com.Contents = CommonInfo.Content;
                    Com.AddTime = CommonInfo.AddTime;
                }
            }
            return Com;
        }


        [HttpGet("IDDown")]
        public Common IDDown(int ID)
        {
            Common Com = new();
            var CommonInfo1 = _db.Commontables.Where(x => x.Id == ID).FirstOrDefault();
            if (CommonInfo1 != null)
            {
                var CommonInfo = _db.Commontables.Where(x => x.ColumnId == CommonInfo1.ColumnId && x.Id < ID).OrderByDescending(x => x.Id).FirstOrDefault();
                if (CommonInfo != null)
                {
                    Com.Id = CommonInfo.Id;
                    Com.ColumnId = CommonInfo.ColumnId;
                    Com.ColumnName = CommonFunction.GetColumnName(_db, CommonInfo.ColumnId);
                    Com.Topic = CommonInfo.Topic;
                    Com.ImgUrl = CommonInfo.ImgUrl;
                    Com.Contents = CommonInfo.Content;
                    Com.AddTime = CommonInfo.AddTime;
                }
            }
            return Com;
        }


        [HttpPost("List")]
        public List<Common> Get(CommonData commonData)
        {
            //int ColumnID = 1, int Begin = 1, int Limit = 15
            List<Common> Com = new();

            var SubIDList = CommonFunction.GetSubID(_db, commonData.ColumnId);

            var CommonInfo = _db.Commontables.Where(x => SubIDList.Contains(x.ColumnId)).ToList().Skip(commonData.Begin - 1).Take(commonData.Limit).ToList();
            if (CommonInfo.Any())
            {
                foreach (var item in CommonInfo)
                {
                    Common c = new()
                    {
                        Id = item.Id,
                        ColumnId = item.ColumnId,
                        Topic = item.Topic,
                        ImgUrl = item.ImgUrl,
                        Contents = item.Content,
                        AddTime = item.AddTime
                    };
                    Com.Add(c);
                }
            }
            return Com;
        }


        [HttpGet("Max")]
        public int Max()
        {
            int MaxID = _db.Commontables.Max(x => x.Id);
            return MaxID;
        }

        [HttpDelete(Name = "Delete")]
        public bool Del(int ID = 0)
        {
            List<CommonInsert> Com = new();
            var CommonInfo = _db.Commontables.FirstOrDefault(x => x.Id == ID);
            if (CommonInfo != null)
            {
                _db.Commontables.Remove(CommonInfo);
            }
            if (_db.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }

        [HttpPost(Name = "Insert")]
        public bool Post(CommonInsert Col)
        {
            Commontable com = new()
            {
                ColumnId = Col.ColumnId,
                Topic = Col.Topic,
                Content = Col.Contents,
                AddTime = Col.AddTime,
            };
            _db.Commontables.Add(com);

            if (_db.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }

        [HttpPut(Name = "Put")]
        public bool Put(Common Col)
        {
            var ColInfo = _db.Commontables.FirstOrDefault(x => x.Id == Col.Id);
            if (ColInfo == null)
            {
                Commontable com = new()
                {
                    ColumnId = Col.ColumnId,
                    Topic = Col.Topic,
                    Content = Col.Contents,
                    AddTime = Col.AddTime,
                };
                _db.Commontables.Add(com);
            }
            else
            {
                ColInfo.ColumnId = Col.ColumnId;
                ColInfo.Topic = Col.Topic;
                ColInfo.Content = Col.Contents;
                ColInfo.AddTime = Col.AddTime;
                _db.Commontables.Attach(ColInfo);
            }

            if (_db.SaveChanges() > 0)
            {
                return true;
            }
            return false;
        }
    }
}